package com.monodaipoly.perfil;


import android.app.Activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MonoDaiPolyActivity extends Activity {
	//private BaseDatosDbAdapter mDbHelper;
	private EditText mNick;
	private EditText mContraseña;
	private static final int REGISTRARSE_ID = Menu.FIRST;
	private static final int SALIR_ID = Menu.FIRST +1;
	
	BDglobal bdglobal;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        bdglobal = ( (BDglobal) getApplicationContext() );
        
        //mDbHelper=new BaseDatosDbAdapter(this);
        //bdglobal.getBaseDatosBD();
    }
    
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, REGISTRARSE_ID, 0, R.string.registrate);
        menu.add(0, SALIR_ID, 0, R.string.salir);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
        switch(item.getItemId()) {
            case REGISTRARSE_ID:
            	Intent intent=new Intent(this,Registro.class);
            	startActivity(intent);
                return true;
            case SALIR_ID:
            	 AlertDialog.Builder builder = new AlertDialog.Builder(this);
                 builder.setMessage("¿Seguro que quieres salir?")
                        .setCancelable(false)
                        .setPositiveButton("Si", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                         	   MonoDaiPolyActivity.this.finish();
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                 dialog.cancel();
                            }
                        });
                 builder.create().show();
                 return true;
            	
        }

        return super.onMenuItemSelected(featureId, item);
    }
    
    
    
    
    
    
    
    
    
    
    public void onClickPerfil(View view){
    	mNick= (EditText) findViewById(R.id.editText1);
    	mContraseña = (EditText) findViewById(R.id.editText2);
    	Cursor c=bdglobal.getBaseDatosBD().comprobarNick(mNick.getText().toString());
    			
    	
    	c.moveToFirst();
    	//startManagingCursor(c);
    	if(c.getCount()>0){
    		if(c.getString(c.getColumnIndex(BaseDatosDbAdapter.KEY_CONTRASEÑA)).compareTo(mContraseña.getText().toString())==0){

        		Toast.makeText(getApplicationContext(), "vamos a irnos al perfil  ", Toast.LENGTH_LONG).show();
        		
        		Intent intent=new Intent(this,Perfil.class);
            	intent.putExtra("id",c.getLong(c.getColumnIndex(BaseDatosDbAdapter.KEY_ROWID)));
            	startActivity(intent);
            	
    		}else{
    			Toast.makeText(getApplicationContext(), "Contraseña incorrecta", Toast.LENGTH_LONG).show();
    		}
    	}else{
    	
    		Toast.makeText(getApplicationContext(), "Nick incorrecto", Toast.LENGTH_LONG).show();
    		
    	}
        
    }
    
    
}